package com.nsgf.library.glide;

import android.app.Activity;
import android.content.Context;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.nsgf.library.R;

import java.io.File;

/**
 * Created by 007 on 2017/9/12.
 */

public class GlideImgManager {

    /**
     * 借助内部类 实现线程安全的单例模式
     * 属于懒汉式单例，因为Java机制规定，内部类SingletonHolder只有在getInstance()
     * 方法第一次调用的时候才会被加载（实现了lazy），而且其加载过程是线程安全的。
     * 内部类加载的时候实例化一次instance。
     */
    public GlideImgManager() {
    }

    private static class GlideLoadUtilsHolder {
        private final static GlideImgManager INSTANCE = new GlideImgManager();
    }

    public static GlideImgManager getInstance() {
        return GlideLoadUtilsHolder.INSTANCE;
    }


    /**
     * load normal  for img
     *
     * @param imgUrl
     * @param erroImg
     * @param emptyImg
     * @param iv
     */
    public void glideLoader(Context context, String imgUrl, int erroImg, int emptyImg, ImageView iv) {

        Glide.with(context).load(imgUrl)
                .placeholder(emptyImg).error(erroImg).into(iv);
    }

    public void glideLoader(Context context, String imgUrl, ImageView iv) {

        Glide.with(context).load(imgUrl).into(iv);
    }

    /**
     * load normal  for  circle or round img
     *
     * @param url
     * @param erroImg
     * @param emptyImg
     * @param iv
     * @param tag
     */
    public void glideLoader(Context context, String url, int erroImg, int emptyImg, ImageView iv, boolean tag) {
        String imgUrl = url;

        if (tag) {
            Glide.with(context).load(imgUrl)
                    .skipMemoryCache(false)
                    .placeholder(emptyImg).error(erroImg).transform(new GlideCircleTransform(context)).into(iv);
        } else {
            Glide.with(context).load(imgUrl)

                    .placeholder(emptyImg).error(erroImg).transform(new GlideRoundTransform(context, 10)).into(iv);
        }
    }

    /**
     * load normal  for  circle or round img
     *
     * @param url
     * @param iv
     * @param tag
     */
    public void glideLoader(Context context, String url, ImageView iv, boolean tag) {
        String imgUrl = url;

        if (tag) {
            Glide.with(context).load(imgUrl)
                    .skipMemoryCache(false)
                    .transform(new GlideCircleTransform(context)).into(iv);
        } else {
            Glide.with(context).load(imgUrl)
                    .transform(new GlideRoundTransform(context, 10)).into(iv);
        }
    }

    /**
     * @param context
     * @param url
     * @param iv
     */

    public void glideLoaderCenterCrop(Context context, String url, ImageView iv) {
        Glide.with(context)
                .load(url)
                .skipMemoryCache(false)
                .into(iv);
    }

    public void glideLoaderCenterHeadCrop(Context context, String imgUrl, ImageView iv) {
        if (context != null) {
            Glide.with(context)
                    .load(imgUrl)
                    .error(R.drawable.ic_nsgf_mine_head_default)
                    .placeholder(R.drawable.ic_nsgf_mine_head_default)
                    .transform(new CenterCrop(), new GlideCircleTransform(context))
                    .into(iv);
        }
    }

    public void glideLoaderCenterHeadCrop(Fragment fragment, String imgUrl, ImageView iv) {
        Glide.with(fragment)
                .load(imgUrl)
                .error(R.drawable.ic_image_nodata)
                .placeholder(R.drawable.ic_image_nodata)
                .transform(new CenterCrop(), new GlideCircleTransform(fragment.getActivity()))
                .into(iv);
    }


    public void glideCenterCrop(Activity activity, String imgUrl, ImageView iv) {

        Glide.with(activity)
                .load(imgUrl)
                .skipMemoryCache(false)
                .into(iv);

    }

    public void glideCenterCrop(Context context, String url, ImageView iv) {

        Glide.with(context)
                .load(url)
                .skipMemoryCache(false)
                .error(R.drawable.ic_image_nodata)
                .placeholder(R.drawable.ic_image_nodata)
                .fitCenter() //刷新后变形问题
                .into(iv);
    }

    public void glideLoaderCenterCrop(Context context, File url, ImageView iv) {
        Glide.with(context)
                .load(url)
                .fitCenter() //刷新后变形问题
                .skipMemoryCache(false)
                .into(iv);
    }

    /**
     * @param context
     * @param url
     * @param iv
     * @param tag
     */
    public void glideLoaderCenterCrop(Context context, String imgUrl, ImageView iv, boolean tag) {

        if (tag) {
            iv.setTag(null);//需要清空tag，否则报错
            Glide.with(context)
                    .load(imgUrl)
                    .transform(new CenterCrop(), new GlideCircleTransform(context))
                    .into(iv);
        } else {
            iv.setTag(null);//需要清空tag，否则报错
            Glide.with(context)
                    .load(imgUrl)
                    .skipMemoryCache(false)
                    .transform(new CenterCrop())
                    .into(iv);
        }
    }

    public void glideLoaderCenterCrop(Context context, String imgUrl, ImageView iv, int size) {
        if (context != null) {
            iv.setTag(null);//需要清空tag，否则报错
            Glide.with(context)
                    .load(imgUrl)
                    .error(R.drawable.ic_image_nodata)
                    .placeholder(R.drawable.ic_image_nodata)
                    .skipMemoryCache(false)
                    .transform(new CenterCrop(), new GlideRoundTransform(context, size)).into(iv);
        }
    }
    public void glideLoaderBannerCenterCrop(Context context, String imgUrl, ImageView iv, int size) {
        if (context != null) {
            iv.setTag(null);//需要清空tag，否则报错
            Glide.with(context)
                    .load(imgUrl)
                    .error(R.drawable.ic_image_banner_nodata)
                    .placeholder(R.drawable.ic_image_banner_nodata)
                    .skipMemoryCache(false)
                    .transform(new CenterCrop(), new GlideRoundTransform(context, size)).into(iv);
        }
    }

    public void glideLoaderCenterCrop(Activity activity, File url, ImageView iv, boolean tag) {
        if (tag) {
            iv.setTag(null);//需要清空tag，否则报错00
            Glide.with(activity)
                    .load(url)
                    .transform(new CenterCrop(), new GlideCircleTransform(activity))
                    .into(iv);
        }
    }
}
